#pragma once

#include "dbcDefine.h"

LIBDBC_BEGIN_NAMESPACE

class IResultSet;

class LIBDBC_API IDatabase
{
protected:
	bool _connected;

public:
	virtual bool freeResultSet(IResultSet* prs) { return false; }
	virtual bool hasResultSet(IResultSet* prs) { return false; }
	virtual bool isConnected() { return _connected; }
	virtual const char* version() { return "none"; }
	virtual const char* getLastError() { return 0; }
	virtual bool commit() { if (_connected) return _commit(); else return false; }
	virtual bool rollback() { if (_connected) return _rollback(); else return false; }

	void use(const char* database) { if (_connected) _use(database); }
	bool close() { bool bClosed = _close(); if (bClosed) { _connected = false; } return bClosed; }

	IResultSet* execQuery(const char* query) { if (_connected) return _execQuery(query); else return NULL; }
	int execInsert(const char* query) { if (_connected) return _execInsert(query); else return 0; }
	int execUpdate(const char* query) { if (_connected) return _execUpdate(query); else return 0; }
	int execDelete(const char* query) { if (_connected) return _execDelete(query); else return 0; }
	int execDML(const char* query) { if (_connected) return _execDML(query); else return 0; }
	
	void beginTransaction() { if (_connected) _beginTransaction(); }

	bool connect(const char* host, const char* database, const char* user, const char* pwd, int port = 0)
	{
		if (isConnected()) close(); return (_connected = _connect(host, database, user, pwd, port));
	}

	bool connect(const char* host, const char* user, const char* pwd, int port = 0)
	{
		if (isConnected()) close(); return (_connected = _connect(host, user, pwd, port));
	}

protected:
	virtual bool _close() { return false; }
	virtual void _use(const char* database) {}
	virtual bool _connect(const char* host, const char* database, const char* user, const char* pwd, int port = 0) { return false; }
	virtual bool _connect(const char* host, const char* user, const char* pwd, int port = 0) { return false; }

	virtual IResultSet* _execQuery(const char* query) { return NULL; }
	virtual void _beginTransaction() { }
	virtual bool _commit() { return false; }
	virtual bool _rollback() { return false; }

	virtual int _execInsert(const char* query) { return 0; }
	virtual int _execUpdate(const char* query) { return 0; }
	virtual int _execDelete(const char* query) { return 0; }
	virtual int _execDML(const char* query) { return 0; }
};


LIBDBC_END_NAMESPACE